package org.seedltd.upms.service.impl.common;

import org.apache.commons.lang3.ArrayUtils;
import org.seedltd.core.jackson.DynamicEnumDTO;
import org.seedltd.core.jackson.DynamicEnumService;
import org.seedltd.upms.constant.GlobalConst;
import org.seedltd.upms.model.sys.entity.SysParam;
import org.seedltd.upms.model.sys.vo.DictEnumVO;
import org.seedltd.upms.service.sys.SysDictService;
import org.seedltd.upms.service.sys.SysParamService;
import org.springframework.stereotype.Service;

import lombok.AllArgsConstructor;

@AllArgsConstructor
@Service("dynamicEnumService")
public class DynamicServiceImpl extends DynamicEnumService {
    private final SysDictService sysDictService;
    private final SysParamService sysParamService;

    @Override
    @SuppressWarnings("unchecked")
    public DynamicEnumDTO getObject(Object value, String type, String[] attached) {
        if (ArrayUtils.contains(attached, GlobalConst.DYNAMIC_TYPE)) {
            SysParam byKey = sysParamService.getByKey(type);
            if (byKey == null) {
                return null;
            }
            return new DynamicEnumDTO().setDesc(byKey.getParamValue()).setValue(byKey.getParamKey());
        }
        DictEnumVO dict = sysDictService.findByTypeAndValue(type, value.toString());
        if (dict == null) {
            return null;
        }
        return new DynamicEnumDTO().setTagType(dict.getTagType()).setDesc(dict.getDesc()).setValue(dict.getValue());
    }
}
